// 给定一个链表和整数k，将链表每个节点向右移动k个位置

// 思路，向右移动k个位置，可以从一个循环链表从头开始第 size - k个节点处断开，下一个节点就是右移后的头节点
// 时间复杂度：O(n)
// 空间复杂度：O(1)

const { LinkedList, ListNode} = require('./64.设计链表')

function rotateRight(head, k) {
    // 边界条件
    if (k === 0 || !head || !head.next) {
        return head
    }
    let count = 1
    let cur = head
    while (cur.next) {
        count++
        cur = cur.next
    }
    cur.next = head // 构成循环链表
    let cut = count - k % count
    while (cut) {
        cur = cur.next
        cut--
    }
    let newHead = cur.next
    cur.next = null
    return newHead
}

let head = [0,1,2,3,5], k = 4
let node = new LinkedList(head)
let res = rotateRight(node.head, k)
console.log(res)